@echo off
setlocal enabledelayedexpansion

:: Set variables
set KUBEVIRT_DIR=kubevirt
set PATCH_FLAG=%KUBEVIRT_DIR%\patch_flag
set PATCHES_CONF=..\patches.conf

:: Clone KubeVirt if it does not exist
if not exist "%KUBEVIRT_DIR%" (
    echo Cloning KubeVirt...
    git clone --branch v1.4.0 https://gitee.com/jianchwa/kubevirt.git
)

cd %KUBEVIRT_DIR%

:: Apply patches if not already applied
if not exist "%PATCH_FLAG%" (
    echo Applying patches...
    for /f "usebackq delims=" %%i in (%PATCHES_CONF%) do (
        set line=%%i
        if not "!line!"=="" if not "!line!"=="#*" (
            echo ==== Applying patch !line! ====
            patch -p1 < ..\!line!
        )
    )
    echo Patch applied successfully.
    echo. > "%PATCH_FLAG%"
) else (
    echo Patches have already been applied.
)

:: Done
echo Script execution completed.
pause
